﻿Imports HMSLibrary
Imports System.Collections.ObjectModel

Public Class PurchaseDataLayer
    Inherits PurchaseDataProvider

    Dim SQLQuery As String = String.Empty

    Public Overrides Function GetSavePOPI(ByVal PType As PurchaseEnum) As System.Collections.Generic.List(Of HMSLibrary.Purchase)

    End Function

    Public Overrides Function SavePOPI(ByVal bill As HMSLibrary.Purchase, ByVal Mode As HMSLibrary.FormAction, ByVal PType As PurchaseEnum) As Integer
        Try
            Dim objprm(6) As Object
            objprm(0) = bill.VendorID
            objprm(1) = bill.PIPODate
            objprm(2) = bill.Remarks
            objprm(3) = bill.SubTotal
            objprm(4) = bill.OtherCharges
            objprm(5) = bill.NetAmt
            objprm(6) = bill.CreatedBy

            If Mode = FormAction.ADD Then
                Dim objTemp As Object = Nothing
                If PType = PurchaseEnum.Invoice Then
                    ReDim Preserve objprm(7)
                    objprm(7) = bill.PIPONo
                    objTemp = DataCommon.db.ExecuteScalar("Proc_InsertPurchaseMaster", objprm)
                End If
                If PType = PurchaseEnum.Order Then objTemp = DataCommon.db.ExecuteScalar("Proc_InsertPurchaseOrder", objprm)
                If objTemp Is Nothing Then Return 0
                bill.PIPOID = objTemp
                MsgBox(CommonMessages.AddMessage, MsgBoxStyle.Information, CommonMessages.MessageTitle)
            ElseIf Mode = FormAction.EDIT Then
                DataCommon.db.ExecuteNonQuery("update_tbl_MyBill", objprm)
                MsgBox(CommonMessages.EditMessage, MsgBoxStyle.Information, CommonMessages.MessageTitle)
            End If
            SaveItemDetails(bill.PIPOID, Mode, bill.PIPOItems, PType)
            If PType = PurchaseEnum.Invoice Then SaveChargesDetails(bill.PIPOID, Mode, bill.PIPODisTax)
        Catch ex As Exception
            Throw ex
            Return 0
        End Try
        Return bill.PIPOID
    End Function

    Private Sub SaveItemDetails(ByVal BillID As Integer, ByVal FormMode As FormAction, ByVal billItems As ObservableCollection(Of BillItems), ByVal PType As PurchaseEnum)
        Dim objprm(0) As Object
        objprm(0) = BillID
        If FormMode = FormAction.EDIT Then
            If PType = PurchaseEnum.Invoice Then DataCommon.db.ExecuteNonQuery("Proc_Delete_PurchaseDetails", objprm)
            If PType = PurchaseEnum.Order Then DataCommon.db.ExecuteNonQuery("Proc_Delete_PurchaseOrderDtl", objprm)
        End If
        ReDim Preserve objprm(7)
        Dim i As Integer = 1
        For Each itm In billItems
            If itm.ItemName.Trim = "" Then GoTo nextItem
            objprm(1) = itm.ItemID
            objprm(2) = itm.Qty
            objprm(3) = itm.UOMID
            objprm(4) = itm.Rate
            objprm(5) = itm.Amount
            objprm(6) = itm.Description
            objprm(7) = i
            If PType = PurchaseEnum.Order Then DataCommon.db.ExecuteNonQuery("Proc_InsertPurchaseOrderDtl", objprm)
            If PType = PurchaseEnum.Invoice Then
                ReDim Preserve objprm(9)
                objprm(7) = ""
                objprm(8) = 0
                objprm(9) = i
                DataCommon.db.ExecuteNonQuery("Proc_InsertPurchaseDetails", objprm)
            End If
            i += 1
nextItem:
        Next
    End Sub

    Private Sub SaveChargesDetails(ByVal BillID As Integer, ByVal FormMode As FormAction, ByVal DisTax As ObservableCollection(Of BillDiscountTax))
        Dim objprm(0) As Object
        objprm(0) = BillID
        If FormMode = FormAction.EDIT Then
            If DataCommon.db.ExecuteNonQuery("Proc_Delete_PurchaseTaxDiscDtl", objprm) = 0 Then Exit Sub
        End If
        ReDim Preserve objprm(3)
        For Each td In DisTax
            If td.TaxDiscName.Trim = "" Then GoTo nextItem
            objprm(1) = td.TaxDiscID
            objprm(2) = td.Rate
            objprm(3) = td.Amount
            If DataCommon.db.ExecuteNonQuery("Proc_InsertPurchaseTaxDiscDtl", objprm) = 0 Then Exit Sub
nextItem:
        Next
    End Sub

    Public Overrides Function GetPurchaseInvoice() As System.Collections.Generic.List(Of HMSLibrary.Purchase)
        Dim lst As New List(Of Purchase)
        Try
            Dim dr As IDataReader
            SQLQuery = "Proc_GetPurchaseMaster"
            dr = DataCommon.db.ExecuteReader(CommandType.StoredProcedure, SQLQuery)
            While dr.Read
                Dim obj As New Purchase
                obj.PIPOID = dr.Item("InvoiceID")
                obj.PIPONo = dr.Item("InvoiceNo")
                obj.VendorID = dr.Item("VendorID")
                obj.VendorName = dr.Item("Name")
                obj.PIPODate = dr.Item("InvDate")
                obj.Remarks = DataCommon.DBNullCheck(dr.Item("Remarks"))
                obj.SubTotal = DataCommon.DBNullCheck(dr.Item("GrossAmt"))
                obj.OtherCharges = dr.Item("TaxDisc")
                obj.NetAmt = dr.Item("NetAmt")
                lst.Add(obj)
            End While
            dr.Close()
        Catch ex As Exception
            Throw ex
        End Try
        Return lst
    End Function
End Class
